package com.zhfi.dataapply.schedule.writer.event;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhfi.dataapply.entity.data.po.event.*;
import com.zhfi.dataapply.entity.data.vo.event.EventAgencyVO;
import com.zhfi.dataapply.entity.data.vo.event.EventVO;
import com.zhfi.dataapply.mapper.event.*;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class EventWriter implements ItemWriter<EventVO> {

    @Autowired
    private EventAgencyMapper eventAgencyMapper;

    @Autowired
    private EventCompanyInfoMapper eventCompanyInfoMapper;

    @Autowired
    private EventInfoMapper eventInfoMapper;

    @Autowired
    private EventExitMapper eventExitMapper;

    @Autowired
    private EventFinanceMapper eventFinanceMapper;

    @Autowired
    private EventInvestMapper eventInvestMapper;

    @Override
    public void write(List<? extends EventVO> list) throws Exception {
        for (EventVO eventVO : list) {
            EventCompanyInfoPO companyInfoPO = eventVO.getCompanyInfo();
            if (null != companyInfoPO) eventCompanyInfoMapper.insert(companyInfoPO);

            EventInfoPO eventInfoPO = eventVO.getEventInfo();
            if (null != eventInfoPO) {
                eventInfoPO.setEventType("18");
                if (eventInfoMapper.updateById(eventInfoPO) <= 0) {
                    eventInfoMapper.insert(eventInfoPO);
                }
            }

            List<EventExitPO> exits = eventVO.getExits();
            if (!CollectionUtils.isEmpty(exits)) {
                exits.forEach(po -> {
                    if (eventExitMapper.updateById(po) <= 0) {
                        eventExitMapper.insert(po);
                    }
                });
            }

            EventFinancePO finance = eventVO.getFinance();
            if (null != finance) {
                if (eventFinanceMapper.updateById(finance) <= 0) {
                    eventFinanceMapper.insert(finance);
                }
            }

            List<EventInvestPO> invests = eventVO.getInvests();
            if (!CollectionUtils.isEmpty(invests)) {
                invests.forEach(po -> {
                    if (eventInvestMapper.updateById(po) <= 0) {
                        eventInvestMapper.insert(po);
                    }
                });
            }

            List<EventAgencyVO> agencys = eventVO.getAgencys();
            if (!CollectionUtils.isEmpty(agencys)) {
                agencys.forEach(agency -> {
                    List<EventAgencyPO> accountings = agency.getAccountings();
                    if (!CollectionUtils.isEmpty(accountings)) {
                        accountings.forEach(po -> {
                            po.setAgencyType("accountings");
                            UpdateWrapper<EventAgencyPO> wrapper = getAgencyUpdateWrapper(
                                    agency.getEventId(), po.getAgencyId());
                            if (eventAgencyMapper.update(po, wrapper) <= 0) {
                                eventAgencyMapper.insert(po);
                            }
                        });
                    }
                    List<EventAgencyPO> assessments = agency.getAssessments();
                    if (!CollectionUtils.isEmpty(assessments)) {
                        assessments.forEach(po -> {
                            po.setAgencyType("assessments");
                            UpdateWrapper<EventAgencyPO> wrapper = getAgencyUpdateWrapper(
                                    agency.getEventId(), po.getAgencyId());
                            if (eventAgencyMapper.update(po, wrapper) <= 0) {
                                eventAgencyMapper.insert(po);
                            }
                        });
                    }
                    List<EventAgencyPO> investBanks = agency.getInvestBanks();
                    if (!CollectionUtils.isEmpty(investBanks)) {
                        investBanks.forEach(po -> {
                            po.setAgencyType("investBanks");
                            UpdateWrapper<EventAgencyPO> wrapper = getAgencyUpdateWrapper(
                                    agency.getEventId(), po.getAgencyId());
                            if (eventAgencyMapper.update(po, wrapper) <= 0) {
                                eventAgencyMapper.insert(po);
                            }
                        });
                    }
                    List<EventAgencyPO> lawFirms = agency.getLawFirms();
                    if (!CollectionUtils.isEmpty(lawFirms)) {
                        lawFirms.forEach(po -> {
                            po.setAgencyType("lawFirms");
                            UpdateWrapper<EventAgencyPO> wrapper = getAgencyUpdateWrapper(
                                    agency.getEventId(), po.getAgencyId());
                            if (eventAgencyMapper.update(po, wrapper) <= 0) {
                                eventAgencyMapper.insert(po);
                            }
                        });
                    }
                    List<EventAgencyPO> sponsors = agency.getSponsors();
                    if (!CollectionUtils.isEmpty(sponsors)) {
                        sponsors.forEach(po -> {
                            po.setAgencyType("sponsors");
                            UpdateWrapper<EventAgencyPO> wrapper = getAgencyUpdateWrapper(
                                    agency.getEventId(), po.getAgencyId());
                            if (eventAgencyMapper.update(po, wrapper) <= 0) {
                                eventAgencyMapper.insert(po);
                            }
                        });
                    }
                });
            }
        }
    }

    private UpdateWrapper<EventAgencyPO> getAgencyUpdateWrapper(String eventId, String agencyId) {
        UpdateWrapper<EventAgencyPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("event_id", eventId);
        updateWrapper.eq("agency_id", agencyId);
        return updateWrapper;
    }
}
